package com.epalmpay.service.bonus.impl;

import com.epalmpay.dto.bonus.*;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.Shop;
import com.epalmpay.mapper.ShopMapper;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.BonusSendRule;
import com.epalmpay.entity.BonusVerifyRule;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.mapper.BonusSendRuleMapper;
import com.epalmpay.mapper.BonusVerifyRuleMapper;
import com.epalmpay.mapper.IndustryMapper;
import com.epalmpay.service.bonus.IBonusRuleService;
import com.epalmpay.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.epalmpay.util.DateUtil.sdfTime;

/**
 * Created by lifayi on 2017/9/22.
 */
@Service
public class BonusRuleServiceImpl implements IBonusRuleService {

    @Autowired
    private BonusSendRuleMapper bonusSendRuleMapper;
    @Autowired
    private BonusVerifyRuleMapper bonusVerifyRuleMapper;
    @Resource
    private IndustryMapper industryMapper;
    @Resource
    private ShopMapper shopMapper;

    @Override
    public BonusRuleIndexOutDTO index(Long groupId) {
        BonusRuleIndexOutDTO bonusRuleIndexOutDTO = new BonusRuleIndexOutDTO();

        List<BonusSendRule> bonusSendRuleList = bonusSendRuleMapper.listByGroupId(groupId);
//        List<BonusVerifyRule> bonusVerifyRuleList = bonusVerifyRuleMapper.listByGroupId(groupId);
        List<BonusVerifyRule> bonusVerifyRuleList = bonusVerifyRuleMapper.listByGroupId(groupId);

        bonusRuleIndexOutDTO.setBonusSendRuleList(bonusSendRuleList);
        bonusRuleIndexOutDTO.setBonusVerifyRuleList(bonusVerifyRuleList);
        bonusRuleIndexOutDTO.setErrorCode(Rescode.SUCCESS);
        return bonusRuleIndexOutDTO;
    }

    @Override
    public BaseOutDTO sendDefaultAdd(BonusSendRule bonusSendRule) {
        Date date = new Date();
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        BonusSendRule bonusSendRule1 = bonusSendRuleMapper.getDefaultByGroupId(bonusSendRule.getGroupId());

        int i;
        if(bonusSendRule1 == null) {
            bonusSendRule.setSendIsDefault(true);
            bonusSendRule.setGmtCreate(date);
            bonusSendRule.setGmtModified(date);
            bonusSendRule.setSendTimeType((byte) ActivityEnum.TimeRangeType.AllDay.getType());
            i = bonusSendRuleMapper.insertSelective(bonusSendRule);
        } else {
            bonusSendRule1.setSendAccountedTime(bonusSendRule.getSendAccountedTime());
            bonusSendRule1.setSendDayCount(bonusSendRule.getSendDayCount());
            bonusSendRule1.setSendDayMax(bonusSendRule.getSendDayMax());
            bonusSendRule1.setSendSingleMax(bonusSendRule.getSendSingleMax());
            bonusSendRule1.setGmtModified(date);
            i = bonusSendRuleMapper.updateByPrimaryKeySelective(bonusSendRule1);
            bonusSendRuleMapper.updateAllIndustry(bonusSendRule.getSendDayCount(), bonusSendRule.getSendDayMax(), bonusSendRule.getGroupId());
        }

        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public BaseOutDTO verifyDefaultAdd(BonusVerifyRule bonusVerifyRule) {
        Date date = new Date();
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        BonusVerifyRule bonusVerifyRule1 = bonusVerifyRuleMapper.getDefaultByGroupId(bonusVerifyRule.getGroupId());

        int i;
        if(bonusVerifyRule1 == null) {
            bonusVerifyRule.setVefityIsDefault(true);
            bonusVerifyRule.setGmtCreate(date);
            bonusVerifyRule.setGmtModified(date);
            bonusVerifyRule.setVerifyTimeType((byte) ActivityEnum.TimeRangeType.AllDay.getType());
            i = bonusVerifyRuleMapper.insertSelective(bonusVerifyRule);
        } else {
            bonusVerifyRule1.setGmtModified(date);
            if(bonusVerifyRule.getSettleRate() !=null){
                bonusVerifyRule1.setSettleRate(bonusVerifyRule.getSettleRate());
            }else{
                bonusVerifyRule1.setSettleRate(0f);
            }
            bonusVerifyRule1.setVerifyDayCount(bonusVerifyRule.getVerifyDayCount());
            bonusVerifyRule1.setVerifyDaySum(bonusVerifyRule.getVerifyDaySum());
            bonusVerifyRule1.setVerifyRate(bonusVerifyRule.getVerifyRate());
            bonusVerifyRule1.setVerifySingleMax(bonusVerifyRule.getVerifySingleMax());
            bonusVerifyRule1.setVerifySingleMaxType(bonusVerifyRule.getVerifySingleMaxType());
            bonusVerifyRule1.setVerifySingleMin(bonusVerifyRule.getVerifySingleMin());
            bonusVerifyRule1.setVerifySingleMinType(bonusVerifyRule.getVerifySingleMinType());
            i = bonusVerifyRuleMapper.updateByPrimaryKey(bonusVerifyRule1);
            bonusVerifyRuleMapper.updateAllIndustry(bonusVerifyRule.getVerifyDaySum(), bonusVerifyRule.getVerifyDayCount(), bonusVerifyRule.getGroupId());
        }

        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public BaseOutDTO industryRuleDel(Map<String, Object> params) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        Long id = (long) (int) params.get("id");
        Long groupId = (long) (int) params.get("groupId");
        int type = (int) params.get("type");

        int i;
        if(type == 1) {
            i = bonusSendRuleMapper.deleteByPrimaryKey2(id, groupId);
        } else {
            BonusVerifyRule result = bonusVerifyRuleMapper.selectByPrimaryKey(id);
            Long shopId = result.getShopId();
            i = bonusVerifyRuleMapper.deleteByPrimaryKey2(id, groupId);
            //删除渠道为门店设置的积分核销规则 如果这个门店有去设置自己的核销积分规则要 去吧结算比例更新成渠道的

            //查询门店自己设置的规则
            BonusVerifyRule bonusVerifyRule = bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(shopId,groupId);
            if(bonusVerifyRule !=null){

                BonusVerifyRule defaultRule = bonusVerifyRuleMapper.getDefaultByGroupId(bonusVerifyRule.getGroupId());
                bonusVerifyRule.setSettleRate(defaultRule.getSettleRate());
                bonusVerifyRuleMapper.updateByPrimaryKeySelective(bonusVerifyRule);

            }



        }

        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public Object industryRule(Map<String, Object> params) {
        Integer type = (Integer) params.get("type");
        Long id = (long) (Integer) params.get("id");
        if(type == 1) {
            return bonusSendRuleMapper.selectByPrimaryKey(id);
        } else {
            return bonusVerifyRuleMapper.selectByPrimaryKey(id);
        }
    }

    @Override
    public BaseOutDTO sendIndustryAdd(BonusSendRule bonusSendRule) {
        BonusSendRule defaultRule = bonusSendRuleMapper.getDefaultByGroupId(bonusSendRule.getGroupId());
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        Date date = new Date();
        int i;
        if(bonusSendRule.getId() == null) {
            bonusSendRule.setGmtCreate(date);
            bonusSendRule.setGmtModified(date);
            bonusSendRule.setSendDayCount(defaultRule.getSendDayCount());
            bonusSendRule.setSendDayMax(defaultRule.getSendDayMax());
            i = bonusSendRuleMapper.insertSelective(bonusSendRule);
        } else {
            bonusSendRule.setGmtModified(date);
            i = bonusSendRuleMapper.updateByPrimaryKeySelective(bonusSendRule);
        }
        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public BaseOutDTO verifyIndustryAdd(BonusVerifyRule bonusVerifyRule) {
        BonusVerifyRule defaultRule = bonusVerifyRuleMapper.getDefaultByGroupId(bonusVerifyRule.getGroupId());
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        Date date = new Date();
        int i;
        if(bonusVerifyRule.getId() == null) {
            bonusVerifyRule.setGmtCreate(date);
            bonusVerifyRule.setGmtModified(date);
            bonusVerifyRule.setType(0);
            if(bonusVerifyRule.getSettleRate() !=null){
                bonusVerifyRule.setSettleRate(bonusVerifyRule.getSettleRate());
            }else{
                bonusVerifyRule.setSettleRate(defaultRule.getSettleRate() ==null? 0:defaultRule.getSettleRate());
            }

            bonusVerifyRule.setVerifyDayCount(defaultRule.getVerifyDayCount());
            bonusVerifyRule.setVerifyDaySum(defaultRule.getVerifyDaySum());
            i = bonusVerifyRuleMapper.insertSelective(bonusVerifyRule);

            //这里渠道为门店新增设置了结算比例  去查看当前门店有没有设置积分核核销规则如有 还得去设置门店的结算比例
            BonusVerifyRule bonusVerifyRule1 = bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(bonusVerifyRule.getShopId(),bonusVerifyRule.getGroupId());
            if(bonusVerifyRule1 !=null){
                if(bonusVerifyRule.getSettleRate() !=null){
                    bonusVerifyRule1.setSettleRate(bonusVerifyRule.getSettleRate());
                }else{
                    bonusVerifyRule1.setSettleRate(defaultRule.getSettleRate());
                }
                bonusVerifyRuleMapper.updateByPrimaryKeySelective(bonusVerifyRule1);
            }


        } else {

            BonusVerifyRule result = bonusVerifyRuleMapper.selectByPrimaryKey(bonusVerifyRule.getId());
            result.setGmtCreate(result.getGmtCreate());
            result.setGmtModified(new Date());
            result.setShopId(bonusVerifyRule.getShopId());
            result.setType(0);
            if(bonusVerifyRule.getSettleRate() !=null){
                result.setSettleRate(bonusVerifyRule.getSettleRate());
            }else{
                result.setSettleRate(defaultRule.getSettleRate());
            }
            result.setVerifyRate(bonusVerifyRule.getVerifyRate());
            result.setVerifyDaySum(defaultRule.getVerifyDaySum());
            result.setVerifyDayCount(defaultRule.getVerifyDayCount());
            result.setVerifySingleMax(bonusVerifyRule.getVerifySingleMax());
            result.setVerifySingleMin(bonusVerifyRule.getVerifySingleMin());
            result.setVerifySingleMaxType(bonusVerifyRule.getVerifySingleMaxType());
            result.setVerifySingleMinType(bonusVerifyRule.getVerifySingleMinType());
            result.setVerifyTimeType(bonusVerifyRule.getVerifyTimeType());
            if(bonusVerifyRule.getVerifyTimeType()==1){
                result.setVerifyTimeFrom1(null);
                result.setVerifyTimeTo1(null);
                result.setVerifyTimeFrom2(null);
                result.setVerifyTimeTo2(null);
                result.setVerifyTimeFrom3(null);
                result.setVerifyTimeTo3(null);
            }else{
                if(bonusVerifyRule.getVerifyTimeFrom1() !=null){
                    result.setVerifyTimeFrom1(bonusVerifyRule.getVerifyTimeFrom1());
                    result.setVerifyTimeTo1(bonusVerifyRule.getVerifyTimeTo1());
                }else{
                    result.setVerifyTimeFrom1(null);
                    result.setVerifyTimeTo1(null);
                }

                if(bonusVerifyRule.getVerifyTimeFrom2() !=null){
                    result.setVerifyTimeFrom2(bonusVerifyRule.getVerifyTimeFrom2());
                    result.setVerifyTimeTo2(bonusVerifyRule.getVerifyTimeTo2());
                }else{
                    result.setVerifyTimeFrom2(null);
                    result.setVerifyTimeTo2(null);
                }

                if(bonusVerifyRule.getVerifyTimeFrom3() !=null){
                    result.setVerifyTimeFrom3(bonusVerifyRule.getVerifyTimeFrom3());
                    result.setVerifyTimeTo3(bonusVerifyRule.getVerifyTimeTo3());
                }else{
                    result.setVerifyTimeFrom3(null);
                    result.setVerifyTimeTo3(null);
                }

            }
            i = bonusVerifyRuleMapper.updateByPrimaryKey(result);

            //这里渠道为门店修改了结算比例  去查看当前门店有没有设置积分核核销规则如有 还得去修改门店的结算比例
            BonusVerifyRule bonusVerifyRule1 = bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(bonusVerifyRule.getShopId(),bonusVerifyRule.getGroupId());
            if(bonusVerifyRule1 !=null){
                if(bonusVerifyRule.getSettleRate() !=null){
                    bonusVerifyRule1.setSettleRate(bonusVerifyRule.getSettleRate());
                }else{
                    bonusVerifyRule1.setSettleRate(defaultRule.getSettleRate());
                }
                bonusVerifyRuleMapper.updateByPrimaryKeySelective(bonusVerifyRule1);
            }

        }
        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public List<Map> getAllIndustry() {
        return industryMapper.listAllOneLevelIndustry();
    }


    @Override
    public List<Map> getAllShop(Long groupId) {
        return shopMapper.allShopList(groupId);
    }

    @Override
    public BonusRuleIndexOutDTO shopVerifyRuleList(BonusRuleInDTO bonusRuleInDTO, Page page) throws Exception {
        BonusRuleIndexOutDTO bonusRuleIndexOutDTO = new BonusRuleIndexOutDTO();
        HashMap<String, Object> param = new HashMap<>();
//        List<Shop> shopList = shopMapper.listShopByGroupId(bonusRuleInDTO.getGroupId());
//        String shopIds="";
//        for(Shop shop:shopList){
//            shopIds +=shop.getId()+",";
//        }
//        shopIds.substring(0,shopIds.length()-1);
        BonusRuleOutDTO bonusRuleOutDTO = bonusVerifyRuleMapper.queryShopBonusVerifyRule(bonusRuleInDTO.getGroupId());
        page.setTotalResult(bonusRuleOutDTO.getNumber());
        if(bonusRuleOutDTO.getNumber()<=0){
            return bonusRuleIndexOutDTO;
        }
//        param.put("shopIds",shopIds);
        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<BonusRuleOutDTO> bonusVerifyRuleList = bonusVerifyRuleMapper.queryShopVerifyRuleList(param);
        bonusRuleIndexOutDTO.setBonusRuleOutDTOList(bonusVerifyRuleList);
        return bonusRuleIndexOutDTO;

//        String shopIds="";
//        int count = 0;
//        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//
//        for(Shop shop:shopList){
//            Map<String,Object> map = new HashMap<>();
//            BonusVerifyRule bonusVerifyRule =bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(shop.getId());
//            if(bonusVerifyRule !=null){
//                count +=1;
//                shopIds +=bonusVerifyRule.getShopId()+",";
//                map.put("id",bonusVerifyRule.getId());
//                map.put("shopName",bonusVerifyRule.getShopName());
//                map.put("verifyRate",bonusVerifyRule.getVerifyRate());
//                map.put("verifySingleMinType",bonusVerifyRule.getVerifySingleMinType());
//                map.put("verifySingleMax",bonusVerifyRule.getVerifySingleMax());
//                map.put("verifySingleMin",bonusVerifyRule.getVerifySingleMin());
//                map.put("verifyDayCount",bonusVerifyRule.getVerifyDayCount());
//                map.put("verifyDaySum",bonusVerifyRule.getVerifyDaySum());
//                map.put("verifyTimeType",bonusVerifyRule.getVerifyTimeType());
//
//                list.add(map);
//            }
//        }
//        if(count<=0){
//            return bonusRuleIndexOutDTO;
//        }
//        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
//
//        page.setTotalResult(count);
//        if(count<page.getShowCount()*page.getCurrentPage()){
//            for(int i=0;i<count;i++){
//                list2.add(list.get(i));
//            }
//        }else{
//
//            for(int i=page.getShowCount()*(page.getCurrentPage()-1);i<list.size()&& i<page.getShowCount()*page.getCurrentPage();i++){
//                list2.add(list.get(i));
//            }
//        }
//        bonusRuleIndexOutDTO.setShopBonusRuleList(list2);


    }


    @Override
    public Void verifyBonusAdd(ShopBonusVerifyRule shopBonusVerifyRule) throws Exception {
        //去查询门店有没有设置积分规则
        int count = bonusVerifyRuleMapper.queryBonusVeriftyRuleByShopId(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());

        BonusVerifyRule result=null;
        result= bonusVerifyRuleMapper.selectBonusSettleRate(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
        if(result ==null){
            result=  bonusVerifyRuleMapper.getDefaultByGroupId(shopBonusVerifyRule.getGroupId());
        }
        if(count==0){
            //查询渠道为门店设置的结算比例


            //去新增
            BonusVerifyRule bonusVerifyRule = new BonusVerifyRule();
            bonusVerifyRule.setGmtCreate(new Date());
            bonusVerifyRule.setGmtModified(new Date());
            bonusVerifyRule.setShopId(shopBonusVerifyRule.getShopId());
            bonusVerifyRule.setType(1);
            bonusVerifyRule.setGroupId(shopBonusVerifyRule.getGroupId());
            bonusVerifyRule.setVerifyRate(shopBonusVerifyRule.getVerifyRate());
            bonusVerifyRule.setSettleRate(result.getSettleRate());
            bonusVerifyRule.setVerifyDaySum(shopBonusVerifyRule.getVerifyDaySum());
            bonusVerifyRule.setVerifyDayCount(shopBonusVerifyRule.getVerifyDayCount());
            bonusVerifyRule.setVerifySingleMax(shopBonusVerifyRule.getVerifySingleMax());
            bonusVerifyRule.setVerifySingleMin(shopBonusVerifyRule.getVerifySingleMin());
            bonusVerifyRule.setVerifySingleMaxType(shopBonusVerifyRule.getVerifySingleMaxType());
            bonusVerifyRule.setVerifySingleMinType(shopBonusVerifyRule.getVerifySingleMinType());
            bonusVerifyRule.setVerifyTimeType(shopBonusVerifyRule.getVerifyTimeType());
            if(shopBonusVerifyRule.getVerifyTimeType()==ActivityEnum.TimeRangeType.TimeRange.getType()){
                if(shopBonusVerifyRule.getVerifyTimeFrom1()!=null){
                    String stringDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeFrom1()+":00";
                    Date date2 = DateUtil.fomatDate(stringDate, sdfTime);
                    bonusVerifyRule.setVerifyTimeFrom1(date2);
                    String toDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeTo1()+":00";
                    bonusVerifyRule.setVerifyTimeTo1(DateUtil.fomatDate(toDate, sdfTime));
                }
                if(shopBonusVerifyRule.getVerifyTimeFrom2()!=null){
                    String stringDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeFrom2()+":00";
                    Date date2 = DateUtil.fomatDate(stringDate, sdfTime);
                    bonusVerifyRule.setVerifyTimeFrom2(date2);
                    String toDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeTo2()+":00";
                    bonusVerifyRule.setVerifyTimeTo2(DateUtil.fomatDate(toDate, sdfTime));
                }
                if(shopBonusVerifyRule.getVerifyTimeFrom3()!=null){
                    String stringDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeFrom3()+":00";
                    Date date2 = DateUtil.fomatDate(stringDate, sdfTime);
                    bonusVerifyRule.setVerifyTimeFrom3(date2);
                    String toDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeTo3()+":00";
                    bonusVerifyRule.setVerifyTimeTo3(DateUtil.fomatDate(toDate, sdfTime));
                }
            }
            bonusVerifyRuleMapper.insertSelective(bonusVerifyRule);

        }else{
            //去编辑
            BonusVerifyRule bonusVerifyRule =bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
            bonusVerifyRule.setGmtCreate(bonusVerifyRule.getGmtCreate());
            bonusVerifyRule.setGmtModified(new Date());
            bonusVerifyRule.setShopId(shopBonusVerifyRule.getShopId());
            bonusVerifyRule.setType(1);
            bonusVerifyRule.setGroupId(shopBonusVerifyRule.getGroupId());
            bonusVerifyRule.setVerifyRate(shopBonusVerifyRule.getVerifyRate());
            bonusVerifyRule.setSettleRate(result.getSettleRate());
            bonusVerifyRule.setVerifyDaySum(shopBonusVerifyRule.getVerifyDaySum());
            bonusVerifyRule.setVerifyDayCount(shopBonusVerifyRule.getVerifyDayCount());
            bonusVerifyRule.setVerifySingleMax(shopBonusVerifyRule.getVerifySingleMax());
            bonusVerifyRule.setVerifySingleMin(shopBonusVerifyRule.getVerifySingleMin());
            bonusVerifyRule.setVerifySingleMaxType(shopBonusVerifyRule.getVerifySingleMaxType());
            bonusVerifyRule.setVerifySingleMinType(shopBonusVerifyRule.getVerifySingleMinType());
            bonusVerifyRule.setVerifyTimeType(shopBonusVerifyRule.getVerifyTimeType());
            if(shopBonusVerifyRule.getVerifyTimeType()==ActivityEnum.TimeRangeType.TimeRange.getType()){
                if(shopBonusVerifyRule.getVerifyTimeFrom1()!=null){
                    String stringDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeFrom1()+":00";
                    Date date2 = DateUtil.fomatDate(stringDate, sdfTime);
                    bonusVerifyRule.setVerifyTimeFrom1(date2);
                    String toDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeTo1()+":00";
                    bonusVerifyRule.setVerifyTimeTo1(DateUtil.fomatDate(toDate, sdfTime));
                }else{
                    bonusVerifyRule.setVerifyTimeFrom1(null);
                    bonusVerifyRule.setVerifyTimeTo1(null);
                }

                if(shopBonusVerifyRule.getVerifyTimeFrom2()!=null){
                    String stringDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeFrom2()+":00";
                    Date date2 = DateUtil.fomatDate(stringDate, sdfTime);
                    bonusVerifyRule.setVerifyTimeFrom2(date2);
                    String toDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeTo2()+":00";
                    bonusVerifyRule.setVerifyTimeTo2(DateUtil.fomatDate(toDate, sdfTime));
                }else{
                    bonusVerifyRule.setVerifyTimeFrom2(null);
                    bonusVerifyRule.setVerifyTimeTo2(null);
                }

                if(shopBonusVerifyRule.getVerifyTimeFrom3()!=null){
                    String stringDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeFrom3()+":00";
                    Date date2 = DateUtil.fomatDate(stringDate, sdfTime);
                    bonusVerifyRule.setVerifyTimeFrom3(date2);
                    String toDate= "1970-01-01 "+shopBonusVerifyRule.getVerifyTimeTo3()+":00";
                    bonusVerifyRule.setVerifyTimeTo3(DateUtil.fomatDate(toDate, sdfTime));
                }else{
                    bonusVerifyRule.setVerifyTimeFrom3(null);
                    bonusVerifyRule.setVerifyTimeTo3(null);
                }
            }else{
                bonusVerifyRule.setVerifyTimeFrom1(null);
                bonusVerifyRule.setVerifyTimeTo1(null);
                bonusVerifyRule.setVerifyTimeFrom2(null);
                bonusVerifyRule.setVerifyTimeTo2(null);
                bonusVerifyRule.setVerifyTimeFrom3(null);
                bonusVerifyRule.setVerifyTimeTo3(null);
            }
            bonusVerifyRuleMapper.updateByPrimaryKey(bonusVerifyRule);
        }
        return null;
    }

    @Override
    public BonusRuleIndexOutDTO shopBonusindex(ShopBonusVerifyRule shopBonusVerifyRule ) {
        BonusRuleIndexOutDTO bonusRuleIndexOutDTO = new BonusRuleIndexOutDTO();
        BonusVerifyRule bonusVerifyRule;
        List<BonusSendRule> bonusSendRuleList = bonusSendRuleMapper.listByGroupId(shopBonusVerifyRule.getGroupId());

        int count = bonusVerifyRuleMapper.queryBonusVeriftyRuleByShopId(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
        //门店自己设置
        if(count==1){
             bonusVerifyRule =bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
        }else{
            //先去上商户给门店设置的如果在没有就去默认的

            bonusVerifyRule=bonusVerifyRuleMapper.selectBonusSettleRate(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
            if(bonusVerifyRule==null){
                //取大商户默认设置
                bonusVerifyRule =bonusVerifyRuleMapper.selectBonusVeriftyRuleByGroupId(shopBonusVerifyRule.getGroupId());
            }
             //结算比例要单独出来
            //看渠道有没有给这个门店设置积分规则 有的话取这个
            BonusVerifyRule bonusVerifyRule1 = bonusVerifyRuleMapper.selectBonusSettleRate(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
            if(bonusVerifyRule1 !=null){
                bonusVerifyRule.setSettleRate(bonusVerifyRule1.getSettleRate());
            }
//            else{
//                bonusVerifyRule1 =bonusVerifyRuleMapper.selectBonusVeriftyRuleByGroupId(shopBonusVerifyRule.getGroupId());
//                bonusVerifyRule.setSettleRate(bonusVerifyRule1.getSettleRate());
//            }
        }
//        List<BonusVerifyRule> bonusVerifyRuleList = bonusVerifyRuleMapper.listByGroupId(groupId);

        bonusRuleIndexOutDTO.setBonusSendRuleList(bonusSendRuleList);
        bonusRuleIndexOutDTO.setBonusVerifyRule(bonusVerifyRule);
//        bonusRuleIndexOutDTO.setBonusVerifyRuleList(bonusVerifyRuleList);
        bonusRuleIndexOutDTO.setErrorCode(Rescode.SUCCESS);
        return bonusRuleIndexOutDTO;
    }

    @Override
    public BonusRuleIndexOutDTO goAddVerifyRule(ShopBonusVerifyRule shopBonusVerifyRule) {
        BonusRuleIndexOutDTO bonusRuleIndexOutDTO = new BonusRuleIndexOutDTO();
        int count = bonusVerifyRuleMapper.queryBonusVeriftyRuleByShopId(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
        BonusVerifyRule bonusVerifyRule;
        //门店自己设置
        if(count==1){
            bonusVerifyRule =bonusVerifyRuleMapper.selectBonusVeriftyRuleByShopId(shopBonusVerifyRule.getShopId(),shopBonusVerifyRule.getGroupId());
        }else{
            //取大商户默认设置
            bonusVerifyRule =bonusVerifyRuleMapper.selectBonusVeriftyRuleByGroupId(shopBonusVerifyRule.getGroupId());
        }
        bonusRuleIndexOutDTO.setBonusVerifyRule(bonusVerifyRule);
        return bonusRuleIndexOutDTO;
    }



}
